package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.MsgTemplateCfgMapper;
import cc.rengu.oltp.service.common.entity.MsgTemplateCfg;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * Created by 王成 on 2020/3/26
 */
public class MsgTemplateCfgMapperImpl implements MsgTemplateCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public MsgTemplateCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertMsgTemplateCfg(MsgTemplateCfg msgTemplateCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMsgTemplateCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, msgTemplateCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记消息模板配置信息失败,instId:<{}>,templateId:<{}>,templateType:<{}>,iReturnCode:<{}>",
                    msgTemplateCfg.getInstId(), msgTemplateCfg.getTemplateId(), msgTemplateCfg.getTemplateType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMsgTemplateCfgByTemplateId(MsgTemplateCfg msgTemplateCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMsgTemplateCfgByTemplateId");
        msgTemplateCfg.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, msgTemplateCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新消息模板配置信息失败,instId:<{}>,templateId:<{}>,templateType:<{}>,iReturnCode:<{}>",
                    msgTemplateCfg.getInstId(), msgTemplateCfg.getTemplateId(), msgTemplateCfg.getTemplateType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MsgTemplateCfg selectMsgTemplateCfgByTemplateId(String instId, String templateId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMsgTemplateCfgByTemplateId");
        MsgTemplateCfg msgTemplateCfg = new MsgTemplateCfg();
        msgTemplateCfg.setInstId(instId);
        msgTemplateCfg.setTemplateId(templateId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, msgTemplateCfg, msgTemplateCfg);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询消息模板配置信息失败,instId:<{}>,templateId:<{}>,iReturnCode:<{}>", instId, templateId, iReturnCode);
            return null;
        }
        return msgTemplateCfg;
    }

    @Override
    public MsgTemplateCfg selectMsgTemplateCfgByTemplateType(String instId, String templateType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMsgTemplateCfgByTemplateType");
        MsgTemplateCfg msgTemplateCfg = new MsgTemplateCfg();
        msgTemplateCfg.setInstId(instId);
        msgTemplateCfg.setTemplateType(templateType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, msgTemplateCfg, msgTemplateCfg);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询消息模板配置信息失败,instId:<{}>,templateType:<{}>,iReturnCode:<{}>", instId, templateType, iReturnCode);
            return null;
        }
        return msgTemplateCfg;
    }
}
